<?php
namespace Admin\Model;
use Think\Model;

class ProjectinoutModel extends AbstractModel {

    public $_validate = array(
        array('ProjectName','require','请输入合同名称',Model::MUST_VALIDATE),
        array('InOut','require','选择收入/支出',Model::MUST_VALIDATE),
        array('Bill','require','金额',Model::MUST_VALIDATE),
        array('HappenDate','require','请选择发生日期',Model::MUST_VALIDATE)
    );

    //获取合同列表
    public function MyshowList(){
        $where = " A.isDel!=1 ";
        $keyword = I('keyword');
        if(!empty($keyword)){
            $where .= " AND E.NowNO like '%$keyword%' ";
        }
        $return = array();
     
        $sql="SELECT 
        COUNT(*)
        FROM 
        projectinout AS A
        LEFT JOIN project AS B ON (A.ProjectID=B.ID) 
        LEFT JOIN `user` AS C ON(A.DoUserID=C.ID)
        LEFT JOIN `user` AS D ON(A.UserID=D.ID)
        LEFT JOIN serialno AS E ON(B.ProjectNO=E.ID)";
        $count = $this->query($sql);
        $count = $count[0]['count'];
        $page = $this->Mypage($count, 8);
        $sql = "SELECT 
        A.ID AS ID,
        E.NowNO AS ProjectID,
        A.`InOut` AS `InOut`,
        A.Bill AS Bill,
        A.HappenDate AS HappenDate,
        C.UserName,
        D.UserName
        FROM 
        projectinout AS A
        LEFT JOIN project AS B ON (A.ProjectID=B.ID) 
        LEFT JOIN `user` AS C ON(A.DoUserID=C.ID)
        LEFT JOIN `user` AS D ON(A.UserID=D.ID)
        LEFT JOIN serialno AS E ON(B.ProjectNO=E.ID)
        WHERE $where ORDER BY A.RecodTime DESC LIMIT  $page->firstRow , $page->listRows";
        $res = $this->query($sql);
        $return['list'] = $res;
        $return['page'] = $page->show('Admin');
        return $return;
    }

    public function Myshow($ID){
        
        $sql = "SELECT 
        A.ID AS ID,
        B.`Name` AS ProjectName,
        B.ID AS ProjectID,
        A.`InOut` AS `InOut`,
        A.Bill AS Bill,
        A.HappenDate AS HappenDate,
        C.ID AS DoUserID,
        C.UserName AS DoUserName,
        A.Memo AS Memo
        FROM 
        projectinout AS A
        LEFT JOIN project AS B ON (A.ProjectID=B.ID) 
        LEFT JOIN `user` AS C ON(A.DoUserID=C.ID)
        LEFT JOIN `user` AS D ON(A.UserID=D.ID)
        LEFT JOIN serialno AS E ON(B.ProjectNO=E.ID)
        WHERE A.ID=".$ID;
        $result=$this->query($sql);
        $Project=D('Project');
        $ProjectName=$Project->field("Name AS Name")->where(array('ID' =>I('ProjectID')))->find();
        $contract=D('contract');
        $ContractName=$contract->field("Title AS Name")->where(array('ID' =>I('ContractID')))->find();
        $result[0]['ProjectName']=$ProjectName['Name'];
        $result[0]['ContractName']=$ContractName['Name'];
        return $result[0];
    }


    public function Myupdata(){
  

            if(!$this->create()){
                $results['msg'] = $this->getError();
                $results['state'] = 0;
            }else{
                $res = $this->save();
                if($res) {
                    $results['status'] = 1;
                    $results['msg'] = "修改成功！";
                    $results['id'] = $res;
                }else{
                    $results['status'] = 0;
                    $results['msg'] = "修改错误！";
                }
            }
        return $results;
    }
       /**
     * 删除业务
     */
    public function MydeletP(){
        $dbName = $this->getTableName();
        $ID = I("ID");
        $sql = " UPDATE {$dbName} SET IsDel = 1 WHERE ID = {$ID}";
        $res = $this->execute($sql);
        if($res){
            $results['state'] = 1;
            $results['msg'] = "删除成功！";
            $results['id'] = $res;
        }else{
            $results['state'] = 0;
            $results['msg'] = "删除错误！";
        }
        return $results;
    }



    // 运算
    public function operation($ID,$Bill){
        // 获取合同金额以及合同类型
        
        $sql1='SELECT InOutType,Bill FROM contract  WHERE id='.$ID;

        $result1=$this->query($sql1);

        //获取 历史支付收入情况

        $sql2='SELECT sum(Bill) AS Bill FROM bill WHERE ContractID='.$ID;

        $result2=$this->query($sql2);
        //收入合同
        if($results[0]['InOutType']==0){
            //获取还剩下需要收的钱
            $Bill=$result1[0]['Bill']-($result2[0]['Bill']+$Bill);
        }else
        {
            //支出合同
            $Bill=$result1[0]['Bill']-($result2[0]['Bill']+$Bill);

        }
        if($Bill>0){
            $results['state'] = 1;
            $results['msg'] = "新增票据成功";
            $results['id'] = $res;
        }else{
            $results['state'] = 0;
            $results['msg'] = "超出合同金额:".$Bill.'元';
        }
        return $results;

    }


        // 合同收入支出类型
    public function InOutType(){
        $array[]=array('ID'=>'0','Name'=>'收入合同');
        $array[]=array('ID'=>'1','Name'=>'支出合同');
        return $array;
    }



    public function Myselect(){
    	//var_dump('123'); exit();
		$where = " 1 ";
		$keyword = I('keyword');
		if(!empty($keyword)){
			$where .= " AND Name like '%$keyword%' ";
		}
		$return = array();
		$sql = " SELECT COUNT(*) count FROM project WHERE $where ";
		$count = $this->query($sql);
		$count = $count[0]['count'];
	    $page = $this->Mypage($count, 8);
		$sql = "SELECT ID,Name,(SELECT NowNO FROM serialno WHERE ID = a.ProjectNO) AS ProjectNO,(SELECT Name FROM department WHERE ID = a.DepID) AS DepID,(SELECT Name FROM datadict WHERE ID = a.ProjectType) AS ProjectType FROM project AS a WHERE IsDel = 0 AND $where ORDER BY ID DESC LIMIT  $page->firstRow , $page->listRows";
		$res = $this->query($sql);
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}

}